package com.zscat.mallplus.bill.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.annotation.SysAdminOperLog;
import com.zscat.mallplus.bill.entity.*;
import com.zscat.mallplus.bill.mapper.BillAftersalesOperateHistoryMapper;
import com.zscat.mallplus.bill.service.*;
import com.zscat.mallplus.oms.entity.OmsOrder;
import com.zscat.mallplus.oms.service.IOmsOrderService;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.ValidatorUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 退货单表 前端控制器
 * </p>
 *
 * @author zscat
 * @since 2019-09-16
 */
@Slf4j
@RestController
@RequestMapping("/bill/billAftersales")
public class BillAftersalesController {
    @Resource
    private IOmsOrderService orderService;
    @Resource
    private IBillAftersalesService billAftersalesService;
    @Resource
    private IBillAftersalesItemsService billAftersalesItemsService;
    @Resource
    private IBillRefundService billRefundService;
    @Resource
    private IBillReshipService billReshipService;

    @Resource
    private IBillExchangeService billExchangeService;
    @Resource
    private BillAftersalesOperateHistoryMapper billAftersalesOperateHistoryMapper;

    @GetMapping(value = "/operateHistoryList")
    public Object operateHistoryList(Long aftersalesId) {
        try {
            List<BillAftersalesOperateHistory> recordList = billAftersalesOperateHistoryMapper.selectList(new QueryWrapper<BillAftersalesOperateHistory>().eq("aftersales_id", aftersalesId));
            return new CommonResult().success(recordList);
        } catch (Exception e) {
        }
        return new CommonResult().failed();
    }

    @SysAdminOperLog(MODULE = "bill", REMARK = "根据条件查询所有退货单表")
    @ApiOperation("根据条件查询所有退货单表")
    @GetMapping(value = "/list")
    public Object getCmsHelpByPage(BillAftersales entity,
                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        try {
            IPage<BillAftersales> page = billAftersalesService.page(new Page<>(pageNum, pageSize), new QueryWrapper<>(entity));
            List<BillAftersales> recordList = new ArrayList<>();
            for (BillAftersales billAftersales : page.getRecords()) {
                // 查询售后商品
                BillAftersalesItems billAftersalesItem = billAftersalesItemsService.getOne(new QueryWrapper<BillAftersalesItems>().eq("aftersales_id", billAftersales.getAftersalesId()));
//                billAftersales.setItemList(billAftersalesItemList);
                OmsOrder order = orderService.getById(billAftersales.getOrderId());
                billAftersales.setOmsOrder(order);
                billAftersales.setBillAftersalesItem(billAftersalesItem);
                recordList.add(billAftersales);
            }
            page.setRecords(recordList);
            return new CommonResult().success(page);
        } catch (Exception e) {
            log.error("根据条件查询所有帮助表列表：%s", e.getMessage(), e);
        }
        return new CommonResult().failed();
    }

    /**
     * 查询退货单明细
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public Object getOmsOrderById(@PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("退货单id");
            }
            // 查询售后单
            BillAftersales billAftersales = billAftersalesService.getById(id);
            // 查询售后商品
            BillAftersalesItems billAftersalesItem = billAftersalesItemsService.getOne(new QueryWrapper<BillAftersalesItems>().eq("aftersales_id", billAftersales.getAftersalesId()));
            billAftersales.setBillAftersalesItem(billAftersalesItem);
            billAftersales.setOmsOrder(orderService.getById(billAftersales.getOrderId()));
            List<BillAftersalesItems> billAftersalesItemList = billAftersalesItemsService.list(new QueryWrapper<BillAftersalesItems>().eq("aftersales_id", billAftersales.getAftersalesId()));
            billAftersales.setItemList(billAftersalesItemList);
            // 退货
            if (billAftersales.getType().equals(2)) {
                BillReship billReship = billReshipService.getOne(new QueryWrapper<BillReship>().eq("aftersales_id", billAftersales.getAftersalesId()));
                BillRefund billRefund = billRefundService.getOne(new QueryWrapper<BillRefund>().eq("aftersales_id", billAftersales.getAftersalesId()));
                billAftersales.setBillRefund(billRefund);
                billAftersales.setBillReship(billReship);
            } else if (billAftersales.getType().equals(3)) { // 换货
                BillExchange billExchange = billExchangeService.getOne(new QueryWrapper<BillExchange>().eq("aftersales_id", billAftersales.getAftersalesId()));
                billAftersales.setBillExchange(billExchange);
            }
            return new CommonResult().success(billAftersales);
        } catch (Exception e) {
            log.error("查询退货单表明细：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
    }

    /**
     * 拒绝退换货
     *
     * @param aftersalesId
     * @return
     */
    @PostMapping(value = "rejectReturn")
    public Object rejectReturn(Long aftersalesId, String remark) {
        try {
            if (ValidatorUtils.empty(aftersalesId)) {
                return new CommonResult().paramFailed("aftersalesId参数为空");
            }
            if (ValidatorUtils.empty(remark)) {
                return new CommonResult().paramFailed("remark参数为空");
            }
            // 查询售后单
            billAftersalesService.doRejectReturn(aftersalesId, remark);
            return new CommonResult().success();
        } catch (Exception e) {
            log.error("查询退货单表明细：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
    }

    /**
     * 确认退换货
     */
    @PostMapping(value = "/confirmReturn")
    public Object confirmReturn(Long aftersalesId, String remark) {
        try {
            if (ValidatorUtils.empty(aftersalesId)) {
                return new CommonResult().paramFailed("aftersalesId参数为空");
            }
            if (ValidatorUtils.empty(remark)) {
                return new CommonResult().paramFailed("remark参数为空");
            }
            billAftersalesService.confirmReturn(aftersalesId, remark);
            return new CommonResult().success();
        } catch (Exception e) {
            log.error("查询退货单表明细：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
    }

    /**
     * 确认收货
     */
    @PostMapping(value = "/confirmReceipt")
    public Object confirmReceipt(Long aftersalesId, String remark) {
        try {
            if (ValidatorUtils.empty(aftersalesId)) {
                return new CommonResult().paramFailed("aftersalesId参数为空");
            }
            if (ValidatorUtils.empty(remark)) {
                return new CommonResult().paramFailed("remark参数为空");
            }
            billAftersalesService.confirmReceipt(aftersalesId, remark);
            return new CommonResult().success();
        } catch (Exception e) {
            log.error("查询退货单表明细：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
    }

    /**
     * 确认发货
     */
    @PostMapping(value = "/handleDeliverGoods")
    public Object handleDeliverGoods(Long aftersalesId, String sellerLogiCode, String sellerLogiNo) {
        try {
            if (ValidatorUtils.empty(aftersalesId)) {
                return new CommonResult().paramFailed("aftersalesId参数为空");
            }
            if (ValidatorUtils.empty(sellerLogiCode)) {
                return new CommonResult().paramFailed("sellerLogiCode参数为空");
            }
            if (ValidatorUtils.empty(sellerLogiNo)) {
                return new CommonResult().paramFailed("sellerLogiNo参数为空");
            }
            billAftersalesService.handleDeliverGoods(aftersalesId, sellerLogiCode, sellerLogiNo);
            return new CommonResult().success();
        } catch (Exception e) {
            log.error("查询退货单表明细：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
    }


}

